<template>
  <div class="knowledge-list">
    <div class="main-content">
      <!-- 分类和搜索区域 -->
      <div class="category-section">
        <div class="nav-title">
          <el-icon>
            <Menu />
          </el-icon>
          知识分类
        </div>
        <div class="nav-list">
          <div class="category-labels">
            <div v-for="category in categories" :key="category.value" class="nav-item"
              :class="{ active: currentCategory === category.label }" @click="handleCategoryClick(category.label)">
              <el-icon>
                <component :is="category.icon || 'Document'" />
              </el-icon>
              <span class="category-name">{{ category.label }}</span>
            </div>
          </div>
          <!-- 搜索框 -->
          <div class="search-area">
            <el-input v-model="searchKeyword" placeholder="搜索感兴趣的养老知识..." :prefix-icon="Search" clearable
              class="search-input" />
          </div>
        </div>
      </div>

      <!-- 文章列表区域 -->
      <div class="article-section">
        <div class="article-header">
          <div class="article-title">
            <el-icon>
              <Document />
            </el-icon>
            {{ currentCategory === '全部' ? '全部文章' : currentCategory }}
          </div>
        </div>

        <div class="article-grid" v-loading="loading">
          <div v-for="article in filteredKnowledge" :key="article.id" class="article-card"
            @click="$router.push(`/knowledge/${article.id}`)">
            <div class="article-content">
              <div class="article-type">
                <el-tag size="small" type="info">养老知识</el-tag>
              </div>
              <h3 class="article-name">{{ article.title }}</h3>
              <p class="article-desc">{{ article.content }}</p>
              <div class="article-meta">
                <div class="meta-left">
                  <span class="meta-item">
                    <el-icon>
                      <Calendar />
                    </el-icon>
                    {{ article.publishDate || '2024-01-01' }}
                  </span>
                  <span class="meta-item">
                    <el-icon>
                      <View />
                    </el-icon>
                    {{ article.views || '0' }}
                  </span>
                </div>
                <div class="meta-right">
                  <el-button type="text" :class="{ 'is-liked': article.isLiked }">
                    <el-icon>
                      <Star />
                    </el-icon>
                    {{ article.rating || '4.9' }}
                  </el-button>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted, computed } from 'vue'
import { Menu, Document, Calendar, View, Star, Search } from '@element-plus/icons-vue'
import api from '@/api'
import { ElMessage } from 'element-plus'

const loading = ref(false)
const searchKeyword = ref('')
const currentCategory = ref('全部')

// 定义分类选项
const categories = [
  { label: '全部', value: 'all', icon: 'Document' },
  { label: '健康知识', value: 'health', icon: 'FirstAidKit', keywords: ['健康', '医疗', '保健'] },
  { label: '生活技巧', value: 'life', icon: 'House', keywords: ['生活', '技巧', '日常'] },
  { label: '政策解读', value: 'policy', icon: 'Document', keywords: ['政策', '法规', '规定'] },
  { label: '心理关怀', value: 'psychology', icon: 'User', keywords: ['心理', '情感', '关怀'] }
]

const knowledgeList = ref([])

// 筛选后的知识列表
const filteredKnowledge = computed(() => {
  let filtered = knowledgeList.value

  // 按分类筛选
  if (currentCategory.value !== '全部') {
    const category = categories.find(cat => cat.label === currentCategory.value)
    if (category) {
      filtered = filtered.filter(article => {
        const searchText = [
          article.title || '',
          article.content || '',
          article.category || ''
        ].join(' ').toLowerCase()
        return category.keywords.some(keyword => searchText.includes(keyword))
      })
    }
  }

  // 按搜索关键词筛选
  if (searchKeyword.value) {
    const keyword = searchKeyword.value.toLowerCase().trim()
    filtered = filtered.filter(article => {
      return article.title?.toLowerCase().includes(keyword) ||
        article.content?.toLowerCase().includes(keyword)
    })
  }

  return filtered
})

// 处理分类点击
const handleCategoryClick = (category) => {
  currentCategory.value = category
}

const fetchKnowledge = async () => {
  loading.value = true
  try {
    const response = await api.get('/knowledge')
    knowledgeList.value = response.data
  } catch (error) {
    console.error('获取养老知识失败:', error)
    ElMessage.error('获取养老知识失败')
  } finally {
    loading.value = false
  }
}

onMounted(() => {
  fetchKnowledge()
})
</script>

<style scoped>
.knowledge-list {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

.main-content {
  flex: 1;
  padding: 20px;
  display: flex;
  flex-direction: column;
  max-width: 1440px;
  margin: 0 auto;
  width: 100%;
}

/* 分类导航样式 */
.category-section {
  background: #fff;
  border-radius: 12px;
  padding: 24px;
  margin-bottom: 30px;
  box-shadow: 0 2px 12px rgba(31, 29, 29, 0.1);
  width: 100%;
}

.nav-title {
  font-size: 24px;
  font-weight: 600;
  color: #333;
  margin-bottom: 30px;
  position: relative;
  width: 100%;
  padding-bottom: 15px;
  border-bottom: 2px solid #f0f0f0;
  display: flex;
  align-items: center;
  gap: 8px;
}

.nav-title::after {
  content: '';
  position: absolute;
  bottom: -2px;
  left: 0;
  width: 100%;
  height: 2px;
  background: linear-gradient(90deg, #1890ff 0%, rgba(24, 144, 255, 0.2) 100%);
  border-radius: 1px;
  z-index: 1;
}

.nav-list {
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.category-labels {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
  gap: 15px;
}

.nav-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 12px 16px;
  border-radius: 6px;
  cursor: pointer;
  transition: all 0.3s ease;
  background: #f5f7fa;
}

.nav-item:hover {
  background: #ecf5ff;
  color: #409eff;
}

.nav-item.active {
  background: #409eff;
  color: #fff;
}

.category-name {
  font-size: 15px;
}

/* 搜索区域样式 */
.search-area {
  margin-top: 20px;
}

/* 文章列表区域样式 */
.article-section {
  background: #fff;
  border-radius: 12px;
  padding: 24px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
  width: 100%;
}

.article-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 30px;
  padding-bottom: 15px;
  border-bottom: 2px solid #f0f0f0;
  position: relative;
}

.article-header::after {
  content: '';
  position: absolute;
  bottom: -2px;
  left: 0;
  width: 100%;
  height: 2px;
  background: linear-gradient(90deg, #1890ff 0%, rgba(24, 144, 255, 0.2) 100%);
  border-radius: 1px;
  z-index: 1;
}

.article-title {
  font-size: 24px;
  font-weight: 600;
  display: flex;
  align-items: center;
  gap: 8px;
  color: #333;
}

.article-grid {
  display: flex;
  flex-direction: column;
  gap: 16px;
  margin-bottom: 30px;
}

.article-card {
  padding: 16px;
  border-radius: 8px;
  background: #fff;
  border: 1px solid #ebeef5;
  cursor: pointer;
  transition: all 0.3s ease;
}

.article-card:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

.article-content {
  display: flex;
  flex-direction: column;
  overflow: hidden;
  padding: 12px;
}

.article-type {
  margin-bottom: 12px;
}

.article-name {
  font-size: 18px;
  font-weight: bold;
  margin-bottom: 12px;
  color: #333;
  display: -webkit-box;
  -webkit-line-clamp: 1;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.article-desc {
  font-size: 14px;
  color: #666;
  margin-bottom: 12px;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.article-meta {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 13px;
  color: #909399;
  margin-top: auto;
}

.meta-left {
  display: flex;
  gap: 15px;
}

.meta-item {
  display: flex;
  align-items: center;
  gap: 4px;
}

.meta-right {
  display: flex;
  gap: 10px;
}

.meta-right .el-button {
  padding: 0;
  height: auto;
}

.is-liked {
  color: #f56c6c;
}

@media screen and (max-width: 768px) {
  .main-content {
    padding: 15px;
  }

  .category-labels {
    grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
  }

  .article-card {
    flex-direction: column;
    height: auto;
    gap: 12px;
  }

  .article-cover {
    width: 100%;
    height: 180px;
  }

  .article-name {
    font-size: 16px;
    margin-bottom: 8px;
  }

  .article-desc {
    margin-bottom: 8px;
  }

  .search-area {
    width: 100%;
  }
}
</style>
